package rexsee.dialog;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.widget.LabelDiv;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Html;
import android.view.KeyEvent;
import android.widget.Toast;

public class RexseeDialog implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Dialog";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeDialog(childBrowser);
	}

	private final Context mContext;
	private final Browser mBrowser;

	public RexseeDialog(Browser browser) {
		super();
		mBrowser = browser;
		mContext = browser.getContext();
	}

	//JavaScript Interface
	public void toast(String message) {
		Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
	}
	public void toast(String message, String style) {

		String defaultStyle = "icon-visibility:false;window-style:dark;window-align:center;window-vertical-align:bottom;top:-20;left:0;background-color:transparent;label-background-color:transparent;border-width:0;";
		LabelDiv label = new LabelDiv(mContext);
		label.setStyle(defaultStyle + style + ";label:" + message + ";");
		if (!label.styleSheet.window_style.equalsIgnoreCase("transparent")) {
			label.setBackgroundResource(label.styleSheet.getWindowStyle());
		}
		Toast toast = new Toast(mContext);
		toast.setView(label);
		toast.setGravity(label.styleSheet.getWindowGravity(), Integer.parseInt(label.styleSheet.left.toLowerCase().replaceAll("px", "")), Integer.parseInt(label.styleSheet.top.toLowerCase().replaceAll("px", "")));
		toast.setDuration(Toast.LENGTH_LONG);
		toast.show();

	}
	public void alert(final String title, final String message) {
		alert(title, message, mContext.getResources().getString(android.R.string.ok), null);
	}
	public void alert(final String title, final String message, final String callback) {
		alert(title, message, mContext.getResources().getString(android.R.string.ok), callback);
	}
	public void alert(final String title, final String message, final String button, final String callback) {
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {

				AlertDialog dialog = new AlertDialog.Builder(mContext).create();
				dialog.setCancelable(false);
				dialog.setOnKeyListener(new Dialog.OnKeyListener() {
					@Override
					public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
						if (arg1 == KeyEvent.KEYCODE_SEARCH) return true;
						else return false;
					}
				});
				dialog.setIcon(mBrowser.application.icon);
				dialog.setTitle(Html.fromHtml(title));
				dialog.setMessage(Html.fromHtml(message));
				dialog.setButton(DialogInterface.BUTTON_POSITIVE, Html.fromHtml(button), new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.dismiss();
						if (callback != null && !callback.equals("")) {
							mBrowser.function.load(callback);
						}
					}
				});
				dialog.show();
			}
		});
	}
	public void confirm(final String title, final String message, final String confirmCallback) {
		confirm(title, message, mContext.getResources().getString(android.R.string.ok), mContext.getResources().getString(android.R.string.cancel), confirmCallback, null);
	}
	public void confirm(final String title, final String message, final String confirmCallback, final String cancelCallback) {
		confirm(title, message, mContext.getResources().getString(android.R.string.ok), mContext.getResources().getString(android.R.string.cancel), confirmCallback, cancelCallback);
	}
	public void confirm(final String title, final String message, final String confirmButton, final String cancelButton, final String confirmCallback, final String cancelCallback) {
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				AlertDialog dialog = new AlertDialog.Builder(mContext).create();
				dialog.setCancelable(false);
				dialog.setOnKeyListener(new Dialog.OnKeyListener() {
					@Override
					public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
						if (arg1 == KeyEvent.KEYCODE_SEARCH) return true;
						else return false;
					}
				});
				dialog.setIcon(mBrowser.application.icon);
				dialog.setTitle(Html.fromHtml(title));
				dialog.setMessage(Html.fromHtml(message));
				dialog.setButton(DialogInterface.BUTTON_NEGATIVE, Html.fromHtml(confirmButton), new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.dismiss();
						if (confirmCallback != null && !confirmCallback.equals("")) {
							mBrowser.function.load(confirmCallback);
						}
					}
				});
				dialog.setButton(DialogInterface.BUTTON_POSITIVE, Html.fromHtml(cancelButton), new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.dismiss();
						if (cancelCallback != null && !cancelCallback.equals("")) {
							mBrowser.function.load(cancelCallback);
						}
					}
				});
				dialog.show();
			}
		});
	}

}
